package com.rockcent.mall.lib.helper;

import com.rockcent.mall.lib.domain.MallFund;
import com.rockcent.mall.lib.domain.MallFundFlow;
import com.rockcent.mall.lib.domain.enumclass.FundTransactionFlow;
import com.rockcent.mall.lib.domain.enumclass.MallFundFlowTransactionType;
import com.rockcent.mall.lib.domain.enumclass.PaymentMethod;

import java.math.BigDecimal;

/**
 * Created by oyhk on 2016/11/25.
 * 平台流程帮助类
 */
public enum MallFundFlowHelper {
    SINGLEONE;


    /**
     * 封装平台资金
     * @param mallFund 平台资金
     * @param mallOrderNum 平台订单号
     * @param changeAvailableAmount 需要改变的可以用金额
     * @param changeFrozen 需要改变的冻结金额
     * @param operationAmount 操作金额
     * @param transactionFlow 交易方向
     * @param transactionType 交易类型
     * @return
     */
    public MallFundFlow create(MallFund mallFund, String mallOrderNum, BigDecimal changeAvailableAmount, BigDecimal changeFrozen, BigDecimal operationAmount, FundTransactionFlow transactionFlow, MallFundFlowTransactionType transactionType) {
        return this.create(mallFund, mallOrderNum, changeAvailableAmount, changeFrozen, operationAmount, transactionFlow, transactionType, null, null);
    }

    /**
     * 封装平台资金
     * @param mallFund 平台资金
     * @param mallOrderNum 平台订单号
     * @param changeAvailableAmount 需要改变的可以用金额
     * @param changeFrozen 需要改变的冻结金额
     * @param operationAmount 操作金额
     * @param transactionFlow 交易方向
     * @param transactionType 交易类型
     * @param merchantOrderNum 商家订单号
     * @param paymentMethod 支付方式
     * @return
     */
    public MallFundFlow create(MallFund mallFund, String mallOrderNum, BigDecimal changeAvailableAmount, BigDecimal changeFrozen, BigDecimal operationAmount, FundTransactionFlow transactionFlow, MallFundFlowTransactionType transactionType, PaymentMethod paymentMethod, String merchantOrderNum) {
        return this.create(mallFund, mallOrderNum, changeAvailableAmount, changeFrozen, operationAmount, transactionFlow, transactionType, paymentMethod, merchantOrderNum,null);
    }

    /**
     * 封装平台资金
     * @param mallFund 平台资金
     * @param mallOrderNum 平台订单号
     * @param changeAvailableAmount 需要改变的可以用金额
     * @param changeFrozen 需要改变的冻结金额
     * @param operationAmount 操作金额
     * @param transactionFlow 交易方向
     * @param transactionType 交易类型
     * @param merchantOrderNum 商家订单号
     * @param paymentMethod 支付方式
     * @return
     */
    public MallFundFlow create(MallFund mallFund, String mallOrderNum, BigDecimal changeAvailableAmount, BigDecimal changeFrozen, BigDecimal operationAmount, FundTransactionFlow transactionFlow, MallFundFlowTransactionType transactionType, PaymentMethod paymentMethod, String merchantOrderNum,Long parentId) {
        MallFundFlow mallFundFlow = new MallFundFlow();
        mallFundFlow.setMallOrderNum(mallOrderNum);

        if (transactionFlow == FundTransactionFlow.INCOME) {
            mallFundFlow.setAvailableAmount(mallFund.getAvailableAmount().add(changeAvailableAmount));
            mallFundFlow.setFrozen(mallFund.getFrozen().add(changeFrozen));
        }
        if (transactionFlow == FundTransactionFlow.EXPEND) {
            mallFundFlow.setAvailableAmount(mallFund.getAvailableAmount().subtract(changeAvailableAmount));
            mallFundFlow.setFrozen(mallFund.getFrozen().subtract(changeFrozen));
        }
        mallFundFlow.setMerchantOrderNum(merchantOrderNum);
        mallFundFlow.setTotalAmount(mallFundFlow.getAvailableAmount().add(mallFundFlow.getFrozen()));// 总金额 = 可用金额 + 冻结金额
        mallFundFlow.setMallFundId(mallFund.getId());
        mallFundFlow.setMallId(mallFund.getMallId());
        mallFundFlow.setOperationAmount(operationAmount);
        mallFundFlow.setTransactionFlow(transactionFlow);
        mallFundFlow.setTransactionType(transactionType);
        mallFundFlow.setRemark(transactionType.getValue());
        mallFundFlow.setPaymentMethod(paymentMethod);
        mallFundFlow.setParentId(parentId);
        return mallFundFlow;
    }

}
